#!/bin/bash
set -e

# 检测系统
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
else
    echo "无法识别操作系统"
    exit 1
fi

echo "检测到系统: $OS"

# 更新系统
update_system() {
    if [[ $OS == "ubuntu" || $OS == "debian" ]]; then
        sudo apt update -y && sudo apt upgrade -y
    elif [[ $OS == "centos" || $OS == "rhel" || $OS == "fedora" ]]; then
        sudo yum update -y
    else
        echo "不支持的系统: $OS"
        exit 1
    fi
}

# 安装 Java 17
install_java() {
    echo "检测 Java 是否已安装..."
    if command -v java &> /dev/null; then
        echo "Java 已安装，跳过安装"
    else
        echo "安装 Java 17..."
        if [[ $OS == "ubuntu" || $OS == "debian" ]]; then
            sudo apt install -y openjdk-17-jdk
        elif [[ $OS == "centos" || $OS == "rhel" || $OS == "fedora" ]]; then
            sudo rpm --import https://packages.adoptium.net/artifactory/api/gpg/key/public
            cat <<EOF | sudo tee /etc/yum.repos.d/adoptium.repo
[Adoptium]
name=Adoptium
baseurl=https://packages.adoptium.net/artifactory/rpm/centos/7/$(uname -m)
enabled=1
gpgcheck=1
gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public
EOF
            sudo yum install -y temurin-17-jdk
        fi
        java -version
    fi
}

# 安装 MySQL 8（通过提供的 RPM 包进行安装）
install_mysql() {
    echo "检测 MySQL 是否已安装..."
    if command -v mysql &> /dev/null; then
        echo "MySQL 已安装，跳过安装"
    else
        echo "安装 MySQL 8..."

        if [[ $OS == "centos" || $OS == "rhel" || $OS == "fedora" ]]; then
            echo "下载并安装 MySQL 8 RPM 包..."
            wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
            sudo rpm -ivh mysql80-community-release-el7-7.noarch.rpm

            echo "安装 MySQL Server..."
            sudo yum install -y mysql-server --nogpgcheck
            sudo yum install -y mysql-community-server

            # 配置 MySQL 的 my.cnf 文件
            sudo tee -a /etc/my.cnf >/dev/null <<EOF

[mysqld]
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
EOF

            # 启动 MySQL 服务并设置开机自启
            sudo systemctl enable mysqld
            sudo systemctl start mysqld

            # 获取 MySQL 初始的 root 密码
            TEMP_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')
            echo "MySQL 初始 root 密码：$TEMP_PASSWORD"

            # 设置 root 密码（可以通过传入的密码进行设置）
            if [[ -n "$MYSQL_ROOT_PASSWORD" ]]; then
                echo "设置 MySQL root 密码为: $MYSQL_ROOT_PASSWORD"
                mysql --connect-expired-password -uroot -p"$TEMP_PASSWORD" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';"
            else
                echo "未提供 root 密码，跳过设置密码"
            fi



            # 输出完成信息
            echo "MySQL 安装完成，并设置为开机自启"
        fi

        if [[ $OS == "ubuntu" || $OS == "debian" ]]; then
            sudo apt install -y mysql-server
            sudo systemctl enable mysql
            sudo systemctl start mysql
        fi
    fi
}

# 安装 Nginx
install_nginx() {
    echo "检测 Nginx 是否已安装..."
    if command -v nginx &> /dev/null; then
        echo "Nginx 已安装，跳过安装"
    else
        echo "安装 Nginx..."
        if [[ $OS == "ubuntu" || $OS == "debian" ]]; then
            sudo apt install -y nginx
        elif [[ $OS == "centos" || $OS == "rhel" || $OS == "fedora" ]]; then
            sudo yum install -y epel-release
            sudo yum install -y nginx
        fi
        sudo systemctl enable nginx
        sudo systemctl start nginx
    fi
}

# 安装 Redis
install_redis() {
    echo "检测 Redis 是否已安装..."
    if command -v redis-server &> /dev/null; then
        echo "Redis 已安装，跳过安装"
    else
        echo "安装 Redis..."
        if [[ $OS == "ubuntu" || $OS == "debian" ]]; then
            sudo apt install -y redis-server
        elif [[ $OS == "centos" || $OS == "rhel" || $OS == "fedora" ]]; then
            sudo yum install -y epel-release
            sudo yum install -y redis
        fi
        sudo systemctl enable redis
        sudo systemctl start redis
    fi
}

# 主执行流程
update_system
install_java
install_mysql
install_nginx
install_redis

echo "✅ 所有组件安装完成！"
